const Websocket = require('ws')
const EventEmitter = require('events')
const signal = new EventEmitter()

const ws = new Websocket('ws://192.168.2.116:8010')

ws.on('open', () => {
    console.log('connect success')
})

ws.on('message', (message) => {
    let data;
    try {
        data = JSON.parse(message)
    } catch (e) {
        console.error('parse error:', e)
    }
    signal.emit(data.event, data.data)
})

function send(event, data) {
    ws.send(JSON.stringify({event, data}))
}

function invoke(event, data, answerEvent) {
    return new Promise((resolve, reject) => {
        send(event, data)
        signal.once(answerEvent, resolve)
        setTimeout(() => {
            reject('timeout')
        }, 5000)
    })
}

signal.send = send
signal.invoke = invoke
module.exports = signal
